package com.qx.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.qx.entity.Users;
import com.qx.util.TokenUtil;
import org.json.JSONObject;
@Component
public class QxInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// 如果不是映射到方法直接通过
				if (!(handler instanceof HandlerMethod)) {
					return true;
				}
				
				
				response.setCharacterEncoding("utf-8");//字符集
				String tokenStr = request.getParameter("token");//接收token
                System.err.println(tokenStr+"不知道为啥");
				if (tokenStr != null && tokenStr != "") {
					try {
						Users u = TokenUtil.unsign(tokenStr, Users.class);
						System.out.println(u+"u的值");
						if (u != null) {
							System.out.println(u+"66666666666666");
							return true;
						}
					} catch (Exception e) {
						
					}
				}
				response.setCharacterEncoding("UTF-8");
				response.setContentType("application/json; charset=utf-8");
				JSONObject json = new JSONObject();
				json.put("success", "false");
				json.put("msg", "认证失败");
				json.put("code", "500");
				response.getWriter().append(json.toString());
				System.out.println("认证失败");
				return false;
			
	}

	
}
